package com.compass.doctor.ui.home.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.compass.doctor.R
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.LayoutAlreadyReceptionOrderItemBinding
import com.compass.doctor.databinding.LayoutCompleteReceptionOrderItemBinding
import com.compass.doctor.databinding.LayoutOnReceptionOrderItemBinding
import com.compass.framework.adapter.BaseBindViewHolder
import com.compass.framework.adapter.BaseMultiItemAdapter
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.saveAs
import com.compass.framework.ext.visible
import com.compass.framework.glide.seCircletUrl
import com.compass.framework.manager.DictionariesManager
import com.compass.framework.manager.UserInfoManager
import com.compass.framework.model.InquiryOrder
import com.compass.framework.utils.BigDecimalUtils

import com.compass.framework.utils.DateUtils
import com.compass.framework.utils.getDrawableResource

/**
 * Description： 问诊单adapter
 * Date:2023/12/12 9:41
 */
class InquiryOrderAdapter(private var itemType: Int) :
    BaseMultiItemAdapter<InquiryOrder>() {

    private lateinit var onCustomButtonClick: OnCustomButtonClick
    fun setBottomCallBack(onChatBottomClick: OnCustomButtonClick) {
        this.onCustomButtonClick = onChatBottomClick
    }

    //用来实现多item布局
    override fun getDefItemViewType(position: Int): Int {
        return itemType
    }


    @SuppressLint("SetTextI18n")
    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<ViewBinding>,
        item: InquiryOrder?,
        position: Int
    ) {
        if (item == null) return
        //  //1、待接诊， 2、已接诊 3、已完成
        when (holder.itemViewType) {
            1 -> {
                val binding = holder.binding.saveAs<LayoutOnReceptionOrderItemBinding>()
                binding.tvInquiryType.text =
                    DictionariesManager.getOrderConsultationTypeString(item.type)
                binding.tvDiagnoseContent.text = item.diseaseDescriptionText
                binding.ivHead.seCircletUrl(item.patientAvatar)
                binding.tvName.text = item.patientName
                binding.tvAge.text = item.patientAge
                binding.ivSex.background =
                    if (item.patientSex == 1) getDrawableResource(R.mipmap.icon_male) else getDrawableResource(
                        R.mipmap.icon_female
                    )
                binding.tvTime.text =
                    "创建时间：${DateUtils.conversionTime(item.createTime, DateUtils.FORMAT_TWO)}"
                binding.tvRefusal.onClick {
                    //拒绝诊断
                    onCustomButtonClick.onButtonCallBack(position, "Refusal")
                }
                binding.tvTransferTreatment.onClick {
                    //转诊
                    onCustomButtonClick.onButtonCallBack(position, "Treatment")
                }

                binding.tvClinicalReception.onClick {
                    //接诊
                    onCustomButtonClick.onButtonCallBack(position, "Reception")
                }
                //1-图文 2-语音 3-音视频
                when (item.type) {
                    "1" -> {
                        binding.imInquiryType.background = getDrawableResource(R.mipmap.icon_tw)
                    }

                    "2" -> {
                        binding.imInquiryType.background = getDrawableResource(R.mipmap.icon_dh)
                    }

                    "3" -> {
                        binding.imInquiryType.background = getDrawableResource(R.mipmap.icon_sp)
                    }
                }

                if (UserInfoManager.getUserInfo()?.appHospitalConfigRespVO?.isAllowReferral == "1") {
                    binding.tvTransferTreatment.visible()
                } else {
                    binding.tvTransferTreatment.gone()
                }


            }

            2 -> {
                val binding = holder.binding.saveAs<LayoutAlreadyReceptionOrderItemBinding>()
                binding.tvInquiryType.text =
                    DictionariesManager.getOrderConsultationTypeString(item.type)
                binding.ivHead.seCircletUrl(item.patientAvatar)
                binding.tvName.text = item.patientName
                binding.tvAge.text = item.patientAge
                binding.ivSex.background =
                    if (item.patientSex == 1) getDrawableResource(R.mipmap.icon_male) else getDrawableResource(
                        R.mipmap.icon_female
                    )
                binding.tvTime.text =
                    "创建时间：${DateUtils.conversionTime(item.createTime, DateUtils.FORMAT_TWO)}"
                binding.tvReceptionTime.text = "接诊时间：${
                    DateUtils.conversionTime(
                        item.consultationStartDate,
                        DateUtils.FORMAT_TWO
                    )
                }"

                binding.tvDiagnosis.text =DictionariesManager.getOrderConsultationTypeString(item.type)
                //1-图文 2-语音 3-音视频
                when (item.type) {
                    "1" -> {
                        binding.imInquiryType.background = getDrawableResource(R.mipmap.icon_tw)
                        binding.tvCallPatient.visible()
                    }

                    "2" -> {
                        binding.imInquiryType.background = getDrawableResource(R.mipmap.icon_dh)
//                        if (item.callNum == 0) binding.tvCallPatient.visible() else binding.tvCallPatient.gone()
                    }

                    "3" -> {
                        binding.imInquiryType.background = getDrawableResource(R.mipmap.icon_sp)
                        binding.tvCallPatient.visible()
                    }
                }

                binding.tvCallPatient.onClick {
                    //1-图文 2-语音 3-音视频
                    when (item.type) {
                        "1" -> {
                            onCustomButtonClick.onButtonCallBack(position, "details")
                        }

                        "2" -> {
                            onCustomButtonClick.onButtonCallBack(position, "callPatient")
                        }

                        "3" -> {
                            onCustomButtonClick.onButtonCallBack(position, "video")
                        }
                    }

                }
                binding.tvCheckOut.onClick {
                    onCustomButtonClick.onButtonCallBack(position, "checkout")
                }

            }

            3 -> {
                val binding = holder.binding.saveAs<LayoutCompleteReceptionOrderItemBinding>()
                binding.tvLongTime.text =
                    "问诊时长/${DateUtils.secondToDate(item.consultationLength)}"

                binding.tvInquiryType.text =
                    DictionariesManager.getOrderConsultationTypeString(item.type)
                binding.ivHead.seCircletUrl(item.patientAvatar)
                binding.tvName.text = item.patientName
                binding.tvAge.text = item.patientAge
                binding.ivSex.background =
                    if (item.patientSex == 1) getDrawableResource(R.mipmap.icon_male) else getDrawableResource(
                        R.mipmap.icon_female
                    )
                binding.tvTime.text =
                    "创建时间：${DateUtils.conversionTime(item.createTime, DateUtils.FORMAT_TWO)}"
                binding.tvEndTime.text = "结束时间：${
                    DateUtils.conversionTime(
                        item.consultationEndDate,
                        DateUtils.FORMAT_TWO
                    )
                }"

                binding.tvDiagnosis.text =
                    "${DictionariesManager.getOrderConsultationTypeString(item.type)}(${
                        BigDecimalUtils.divide(item.payPrice)
                    }元)"
                //1-图文 2-语音 3-音视频
                when (item.type) {
                    "1" -> {
                        binding.tvCallPatient.gone()
                        binding.imInquiryType.background = getDrawableResource(R.mipmap.icon_tw)
                    }

                    "2" -> {
                        binding.tvCallPatient.visible()
                        binding.imInquiryType.background = getDrawableResource(R.mipmap.icon_dh)
                    }

                    "3" -> {
                        binding.tvCallPatient.gone()
                        binding.imInquiryType.background = getDrawableResource(R.mipmap.icon_sp)
                    }
                }

                binding.tvCallPatient.onClick {
                    //1-图文 2-语音 3-音视频
                    when (item.type) {
                        "1" -> {
                            onCustomButtonClick.onButtonCallBack(position, "details")
                        }

                        "2" -> {
                            onCustomButtonClick.onButtonCallBack(position, "callPatient")
                        }

                        "3" -> {
                            onCustomButtonClick.onButtonCallBack(position, "video")
                        }
                    }

                }

                binding.tvRecords.onClick {
                    onCustomButtonClick.onButtonCallBack(position, "records")

                }
            }

        }


    }


    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewBinding {
        return when (viewType) {
            1 -> {
                LayoutOnReceptionOrderItemBinding.inflate(layoutInflater, parent, false)
            }

            2 -> {
                LayoutAlreadyReceptionOrderItemBinding.inflate(layoutInflater, parent, false)
            }

            else -> {
                LayoutCompleteReceptionOrderItemBinding.inflate(layoutInflater, parent, false)
            }
        }

    }
}